//
//  MyEvaluationViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2018/1/17.
//  Copyright © 2018年 史宵宵. All rights reserved.
//

#import "MyEvaluationViewController.h"
#import "MyEvaluationInfo.h"
#import "XHStarRateView.h"

@interface MyEvaluationViewController ()<XHStarRateViewDelegate>
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,copy) NSString *isPublish;
@end

@implementation MyEvaluationViewController
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    //    [Global setStatusBarBackgroundColor:[UIColor clearColor]];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"我的自评";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self creatDataList];
    
}
- (void)creatFooterView{
    UIView *footView = [[UIView alloc]init];
    footView.frame = CGRectMake(0, 0, kScreenW, 100);
    footView.backgroundColor = [UIColor whiteColor];
    
    CompleteButton *completeButton = [[CompleteButton alloc]initWithFrame:CGRectMake(10, 10, kScreenW - 20, 50) bgColor:KBACKNAVIGATIONCOLOR tColor:[UIColor whiteColor] text:@"提交"];
    [completeButton addTarget:self action:@selector(addEvaluation) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:completeButton];
//    completeButton.userInteractionEnabled = self.isPublish;
    completeButton.backgroundColor = KBACKNAVIGATIONCOLOR;
    self.tableView.tableFooterView = footView;
}
#pragma mark - 提交我的评价
- (void)addEvaluation{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    NSData *dataFriends = [NSJSONSerialization dataWithJSONObject:[NSMutableArray mj_keyValuesArrayWithObjectArray:self.dataArray] options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:dataFriends encoding:NSUTF8StringEncoding];
    [dic setValue:jsonString forKey:@"answer"];    //评分 "{"key":"classId 科目值","score":"分数"}"
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [dic setValue:self.date forKey:@"date"]; //用户的token
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppInterestSaveEvaluateAnswer] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            [Global showActivityWithView:self.view withText:@"评价成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}
- (void)creatDataList{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [dic setValue:self.date forKey:@"date"]; //用户的token
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppInterestGetEvaluateAnswer] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            self.isPublish = [NSString stringWithFormat:@"%@",response[@"isPublish"]];
            [self.dataArray addObjectsFromArray:[MyEvaluationInfo mj_objectArrayWithKeyValuesArray:response[@"list"]]];
            if (![self.isPublish isEqualToString:@"1"]) {
                [self creatFooterView];
            }
            [self.tableView reloadData];
        }
    }];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"cell1";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //        if (cell == nil) {
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    //        }
    
    MyEvaluationInfo *myEvaluationInfo = self.dataArray[indexPath.row];
    
    UILabel *desLabel = [[UILabel alloc]init];
    desLabel.text = myEvaluationInfo.content;
    desLabel.frame = CGRectMake(10, 20, 100, 30);
    desLabel.textColor = [UIColor whiteColor];
    desLabel.backgroundColor = KBACKNAVIGATIONCOLOR;
    desLabel.textAlignment = NSTextAlignmentCenter;
    desLabel.clipsToBounds = YES;
    desLabel.font = [UIFont systemFontOfSize:14];
    desLabel.layer.cornerRadius = 15;
    [cell.contentView addSubview:desLabel];
    
    NSString *scoreStr = myEvaluationInfo.answer;
    scoreStr = strIsEmpty(scoreStr)? @"0": scoreStr;
//    NSInteger scoreInteger = strIsEmpty(scoreStr) ? 0: [scoreStr integerValue];
    XHStarRateView *starRateView = [[XHStarRateView alloc]init];
    starRateView.isMove = [self.isPublish isEqualToString:@"1"]?NO:YES;
    starRateView.rateStyle = WholeStar;
    starRateView.tag = 1000+indexPath.row;
    starRateView.frame = CGRectMake(kScreenW - 160, 22, 150, 25);
    [starRateView setStarScore:scoreStr];
    starRateView.delegate = self;
    [cell.contentView addSubview:starRateView];
    //        self.starView = starRateView;
    
    UILabel *scoreLabel = [[UILabel alloc]init];
    scoreLabel.frame = CGRectMake(10, 50 + 5, kScreenW - 20, [Global getSizeOfString:myEvaluationInfo.explain maxWidth:kScreenW - 20 maxHeight:MAXFLOAT withFontSize:14].height);
    scoreLabel.textAlignment = NSTextAlignmentLeft;
    scoreLabel.font = [UIFont systemFontOfSize:14];
    scoreLabel.text = myEvaluationInfo.explain;
    scoreLabel.numberOfLines = 0;
    scoreLabel.textColor =  KHEADERTITLECOLOR;
    [cell.contentView addSubview:scoreLabel];
        //        self.scoreLabel = scoreLabel;

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyEvaluationInfo *myEvaluationInfo = self.dataArray[indexPath.row];
    
    return 60 + [Global getSizeOfString:myEvaluationInfo.explain maxWidth:kScreenW - 20 maxHeight:MAXFLOAT withFontSize:14].height;
}
#pragma mark - 选择星星
-(void)starRateView:(XHStarRateView *)starRateView currentScore:(CGFloat)currentScore{
//    NSLog(@"%ld----  %f",starRateView.tag- 1000,currentScore);
    NSInteger index = starRateView.tag- 1000;
    MyEvaluationInfo *myEvaluationInfo = self.dataArray[index];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[NSString stringWithFormat:@"%.0f",currentScore] forKey:@"answer"];
    [dic setValue:myEvaluationInfo.content forKey:@"content"];
    [dic setValue:myEvaluationInfo.explain forKey:@"explain"];
    [self.dataArray replaceObjectAtIndex:index withObject:[MyEvaluationInfo mj_objectWithKeyValues:dic]];
    [self.tableView reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
